using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Caching;

namespace ClownFish
{
	public static class XmlCommandManager
	{
		[CompilerGenerated]
		private sealed class Class0
		{
			public Dictionary<string, XmlCommand> dictionary_0;

			public void method_0(XmlCommand xmlCommand_0)
			{
				this.dictionary_0.smethod_0(xmlCommand_0.CommandName, xmlCommand_0);
			}
		}

		private static readonly string string_0 = Guid.NewGuid().ToString();

		private static Exception exception_0 = null;

		private static Dictionary<string, XmlCommand> dictionary_0 = null;

		[MethodImpl(MethodImplOptions.Synchronized)]
		public static void LoadCommnads(string directoryPath)
		{
			if (XmlCommandManager.dictionary_0 != null && Class29.bool_0)
			{
				throw new InvalidOperationException("不允许重复调用这个方法。");
			}
			if (!Directory.Exists(directoryPath))
			{
				throw new DirectoryNotFoundException(string.Format("目录 {0} 不存在。", directoryPath));
			}
			Exception ex = null;
			XmlCommandManager.dictionary_0 = XmlCommandManager.smethod_1(directoryPath, out ex);
			if (ex != null)
			{
				XmlCommandManager.exception_0 = ex;
			}
			if (XmlCommandManager.exception_0 != null)
			{
				throw XmlCommandManager.exception_0;
			}
		}

		internal static void smethod_0<T, U>(this Dictionary<T, U> dictionary_1, T gparam_0, U gparam_1)
		{
			try
			{
				dictionary_1.Add(gparam_0, gparam_1);
			}
			catch (ArgumentException innerException)
			{
				throw new ArgumentException(string.Format("往集合中插入元素时发生了异常，当前Key={0}", gparam_0), innerException);
			}
		}

		private static Dictionary<string, XmlCommand> smethod_1(string string_1, out Exception exception_1)
		{
			Action<XmlCommand> action = null;
			XmlCommandManager.Class0 @class = new XmlCommandManager.Class0();
			exception_1 = null;
			@class.dictionary_0 = null;
			try
			{
				string[] files = Directory.GetFiles(string_1, "*.config", SearchOption.AllDirectories);
				if (files.Length > 0)
				{
					@class.dictionary_0 = new Dictionary<string, XmlCommand>(2048);
					string[] array = files;
					for (int i = 0; i < array.Length; i++)
					{
						string path = array[i];
						List<XmlCommand> list = XmlHelper.XmlDeserializeFromFile<List<XmlCommand>>(path, Encoding.UTF8);
						List<XmlCommand> arg_65_0 = list;
						if (action == null)
						{
							action = new Action<XmlCommand>(@class.method_0);
						}
						arg_65_0.ForEach(action);
					}
				}
			}
			catch (Exception ex)
			{
				exception_1 = ex;
				@class.dictionary_0 = null;
			}
			if (Class29.bool_0)
			{
				CacheDependency dependencies = new CacheDependency(string_1);
				HttpRuntime.Cache.Insert(XmlCommandManager.string_0, string_1, dependencies, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(XmlCommandManager.smethod_2));
			}
			return @class.dictionary_0;
		}

		private static void smethod_2(string string_1, object object_0, CacheItemRemovedReason cacheItemRemovedReason_0)
		{
			Exception ex = null;
			string string_2 = (string)object_0;
			for (int i = 0; i < 5; i++)
			{
				Thread.Sleep(3000);
				Dictionary<string, XmlCommand> dictionary = XmlCommandManager.smethod_1(string_2, out ex);
				if (ex == null)
				{
					try
					{
					}
					finally
					{
						XmlCommandManager.dictionary_0 = dictionary;
						XmlCommandManager.exception_0 = null;
					}
					return;
				}
			}
			if (ex != null)
			{
				XmlCommandManager.exception_0 = ex;
			}
		}

		public static XmlCommand GetCommand(string name)
		{
			if (XmlCommandManager.exception_0 != null)
			{
				throw XmlCommandManager.exception_0;
			}
			if (XmlCommandManager.dictionary_0 == null)
			{
				return null;
			}
			XmlCommand result;
			if (XmlCommandManager.dictionary_0.TryGetValue(name, out result))
			{
				return result;
			}
			return null;
		}
	}
}
